import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'


const routes = [
  {
    path: '/login',
    component: () => import('@/views/login/LoginPage.vue'),
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../views/404.vue')
  },
  // {
  //   path: '/inform',
  //   component: () => import('../views/Inform.vue'),
  // }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 提供一个重置路由的方法
export const resetRouter = () => {
  // 重新创建路由实例
  const newRouter = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes
  })
  router.matcher = newRouter.matcher // reset router
}

// 注意：刷新页面会导致页面路由重置
export const setRoutes = () => {

  // router.addRoute({
  //   path: '/admin',
  //   component: () => import('@/views/layout/LayoutContainer.vue'),
  //   redirect: '/admin/user/userinfo',
  //   children: [{
  //     path: '/admin/user/password',
  //     component: () => import('@/views/user/UserPassword.vue')
  //   },
  //   {
  //     path: '/admin/user/avatar',
  //     component: () => import('@/views/user/UserAvatar.vue')
  //   },
  //   {
  //     path: '/admin/user/profile',
  //     component: () => import('@/views/user/UserProfile.vue')
  //   }]
  // })
  const storeMenus = localStorage.getItem("menus");
  console.log("在setRoutes中拿出的菜单时",storeMenus);
  if (storeMenus) {

    // 获取当前的路由对象名称数组
    const currentRouteNames = router.getRoutes().map(v => v.name)
    if (!currentRouteNames.includes('Manage')) {
      // 拼装动态路由
      const manageRoute =  {
        path: '/',
        component: () => import('@/views/layout/LayoutContainer.vue'),
        redirect: '/login',
        children: [{
          path: 'admin/user/password',
          component: () => import('@/views/user/UserPassword.vue')
        },
        {
          path: 'admin/user/avatar',
          component: () => import('@/views/user/UserAvatar.vue')
        },
        {
          path: 'admin/user/profile',
          component: () => import('@/views/user/UserProfile.vue')
        }]
      }
      const menus = JSON.parse(storeMenus)
      menus.forEach(item => {
        if (item.path) {  // 当且仅当path不为空的时候才去设置路由
          let itemMenu = { path: item.path.replace("/", ""), name: item.name, component: () => import(`@/views/commodity/${item.pagePath}.vue` )}
          manageRoute.children.push(itemMenu)
        } else if(item.children && item.children.length) {
          item.children.forEach(childItem => {
            if (childItem.path) {
              let itemMenu = { path: childItem.path.replace("/", ""), name: childItem.name, component: () => import(`@/views/commodity/${childItem.pagePath}.vue`)}
              manageRoute.children.push(itemMenu)
            }
          })
        }
      })
      
      console.log("路径输出",manageRoute );
      // 动态添加到现在的路由对象中去
      router.addRoute(manageRoute)
    }

  }
}

// 重置我就再set一次路由
setRoutes()

// 路由守卫
router.beforeEach((to) => {
  const token = useUserStore().token
  if (!token && to.path !== '/login') return '/login'
})

export default router
